<?php
namespace backend\model;

use backend\model\role_auth;
use backend\model\admin_access;
use common\model\common_model;
/**
 * admin模型数据库操作类
 */

class admin_model extends common_model {
	public $table_name = 'admin';
	public $category   = '';
	public $_pk         = 'admin_id';
	public function __construct()
    {
		$this->db_config  = load_config('database');
		$this->db_setting = 'default';
        parent::__construct();
	    
	}
	//设置表名
	public function set_table($param=[]) {

		// $this->table_name = $this->db_tablepre.$this->model[$param['modelid']]['tablename'];

	}

    /**
     * 用户登录
     * @param $params
     * @return bool
     * @throws Exception
     */
    public function login($params)
    {
        if(empty($params['username'])){
            throw new \Exception('用户名不能为空',-1);
        }
        if(empty($params['password'])){
            throw new \Exception('密码不能为空',-2);
        }
        $admin = $this->getOneByName($params['username']);
        if(!$admin){
            throw new \Exception('管理员不存在',-100);
        }

        if($this->hashPassword($params['password']) == $admin['password']){
            return true;
        }
        throw new \Exception('密码错误',-200);
    }

    private function hashPassword($str){
        return md5($str);
    }

	public function getOneByName($username)
    {
        return $this->get_one(['username'=>$username],'username,password,role_id');
    }

    /**
     * 根据角色id获取权限
     * @param $role_id
     * @return array
     */
    public function getRoleAuthList($role_id)
    {
        $role_auth_model = new role_auth();
        $role_auth_list = $role_auth_model->select(['role_id'=>$role_id],'*','','','','access_id');
        if(empty($role_auth_list)){
            return false;
        }
        $access_id_arr = array_keys($role_auth_list);
        $admin_access_model = new admin_access();
        $rules_list = $admin_access_model->select(['in','access_id',$access_id_arr],'*','','','');
        $rules_list[] = [
           'access_id'  =>'0',
           'is_show'     =>'1',
           'access_name'=>'审核文章',
           'access_rule'=>'/backend/article/verify',
           'update_time'=>'1572768143',
        ];
        return $rules_list;
    }

    /**
     * 当前的角色对某路径是否有权限
     * @param $role_id    角色id
     * @param $path_info  访问的路径
     * @return bool
     */
    public function checkAuth($auth_list,$path_info)
    {
        if(!$auth_list){
            return false;
        }
        if($path_info == '/backend/index/index'){
            return true;
        }

        $allow_url_list = [];
        foreach($auth_list AS $k=>$v){
            $allow_url_list[] = $v['access_rule'];
        }

        if(in_array($path_info,$allow_url_list)){
            return true;
        }

        foreach($allow_url_list AS $k=>&$v){
            $v = str_replace("/",'\/',$v);
            $pat ="/".$v."/i";
            if(preg_match($pat,$path_info)){
                return true;
            }
        }
        return false;
    }


}
?>